package com.choosefine.base.shixiaobao.common.conf;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.YamlMapFactoryBean;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

/**
 * Created by Administrator on 2016/11/10.
 */
@Configuration
public class BeanConfig {
	private final Logger logger = LoggerFactory.getLogger("【yaml文件的配置读取】");
	private Map<String, Map> configAsMap = new HashMap<String, Map>();
	private Map<String, Properties> configAsProperties = new HashMap<String, Properties>();

	@Bean
	public Properties yaml() {
		ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
		YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
		Properties resultProperties = null;
		try {
			logger.info("从<配置文件>中<>读取");
			Resource[] resource = resolver.getResources("classpath*:/**/*.yml");
			
			logger.info("-------------->" + resource.length);
			yaml.setResources(resource);
			resultProperties = yaml.getObject();
		} catch (Exception e) {
			logger.error("无法读取 yaml 文件", e);
			resultProperties = null;
		}
		return resultProperties;
	}

	/**
	 * 从yaml中读取配置
	 * @param yamlSource
	 * @return
	 * @author guwenwei
	 * @date 2016/11/29 5:49
	 */
	public Map<String, Object> yaml2Map(String yamlSource) {
		Map resultMap = null;
		try {
			if (configAsMap.containsKey(yamlSource.trim())) {
				logger.info("直接从内存中读取");
				resultMap = configAsMap.get(yamlSource.trim());
			} else {
				logger.info("从配置文件中读取");
				YamlMapFactoryBean yaml = new YamlMapFactoryBean();
				yaml.setResources(new ClassPathResource(yamlSource));
				resultMap = yaml.getObject();
				configAsMap.put(yamlSource, resultMap);
			}
		} catch (Exception e) {
			logger.error("无法读取 yaml 文件", e);
			resultMap = null;
		}
		return resultMap;
	}

	/**
	 * 从property中读取配置
	 * @param yamlSource
	 * @return
	 * @author guwenwei
	 * @date 2016/11/29 5:49
	 */
	public Properties yaml2Properties(String yamlSource) {
		Properties resultProperties = null;
		try {
			if (configAsProperties.containsKey(yamlSource.trim())) {
				logger.info("直接从<内存>中<>读取");
				resultProperties = configAsProperties.get(yamlSource.trim());
			} else {
				logger.info("从<配置文件>中<>读取");
				YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
				yaml.setResources(new ClassPathResource(yamlSource));
				resultProperties = yaml.getObject();
				configAsProperties.put(yamlSource, resultProperties);
			}
		} catch (Exception e) {
			logger.error("无法读取 yaml 文件", e);
			resultProperties = null;
		}
		return resultProperties;
	}

	/**
	 * 将yml转换为properties
	 * @param yamlSource
	 * @param key
	 * @return
	 * @author guwenwei
	 * @date 2016/11/29 5:49
	 */
	public Properties yaml2Properties(String yamlSource, String key) {
		Properties resultProperties = new Properties();
		Map map = yaml2Properties(yamlSource);
		for (Object propertyKey : map.keySet()) {
			if ((propertyKey + "").startsWith(key + ".")) {
				resultProperties.put(propertyKey, map.get(propertyKey));
			}
		}
		return resultProperties;
	}
	//叶文达
//	@Value("${dmsUrl}")
//	final String dmsUrl = null;
//
//	@Value("${server.context-path}")
//	final String serviceName = null;
//
//	@Bean
//	public UrlRegistrar urlRegistrar(RequestMappingHandlerMapping requestMappingHandlerMapping, RestTemplate restTemplate) {
//	    // 在设定了dmsUrl的情况下，去注册URLs
//	    if (!StringUtils.isEmpty(dmsUrl) && dmsUrl.startsWith("http")) {
//	        return new UrlRegistrar(requestMappingHandlerMapping, restTemplate, serviceName, dmsUrl);
//	    }
//	    return null;
//	}
//	@Bean
//	public RequestMappingHandlerMapping requestMappingHandlerMapping() {
//	    return new RequestMappingHandlerMapping();
//	}
}
